# coding=utf-8
import os

__author__ = 'yfyang'

import MySQLdb

#  database config

db_config = {
    'user': 'oulet',
    'password': 'oulet@ifoly',
    'host': '27.54.253.91',
    'database': 'oulet',
    'raise_on_warnings': True
}

floor_config = {
    '2F-A': 12,
    '2F-B': 13,
    '3F-A': 14,
    '3F-B': 15,
    '4F-A': 16,
    '4F-B': 17,
    '5F': 18,
    'B2' : 10
}


def insert_floor(data_file):
    """

    Args:


    Returns:

    """
    f = open(data_file)
    floor_id = floor_config[os.path.basename(data_file).split(".")[0]]
    line = f.readline().replace('\r\n', '')
    values = []
    while line:
        print line
        dline = line.split('|')
        values.append((floor_id, dline[0], dline[1]))
        line = f.readline().replace('\r\n', '')
    try:
        conn = MySQLdb.connect(db_config['host'], db_config['user'], db_config['password'], db_config['database'])
        cur = conn.cursor()
        cur.executemany('insert into olb_parking (floor, mark, location, mall) values (%s,%s,%s,1)', values)
        conn.commit()
        cur.close()
        conn.close()
    except MySQLdb.Error, e:
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])

# 车位导入信息


if __name__ == "__main__":
    # insert_floor('/Users/yfyang/Workthing/03_ifolf/zncs/misc/tools/data/2F-A.txt')
    # insert_floor('/Users/yfyang/Workthing/03_ifolf/zncs/misc/tools/data/2F-B.txt')
    # insert_floor('/Users/yfyang/Workthing/03_ifolf/zncs/misc/tools/data/3F-A.txt')
    # insert_floor('/Users/yfyang/Workthing/03_ifolf/zncs/misc/tools/data/3F-B.txt')
    # insert_floor('/Users/yfyang/Workthing/03_ifolf/zncs/misc/tools/data/4F-A.txt')
    # insert_floor('/Users/yfyang/Workthing/03_ifolf/zncs/misc/tools/data/4F-B.txt')
    # insert_floor('/Users/yfyang/Workthing/03_ifolf/zncs/misc/tools/data/5F.txt')
    insert_floor('/Users/yfyang/Workthing/03_ifolf/zncs/misc/tools/data/B2.txt')

